﻿using Newtonsoft.Json.Linq;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace Pay.Token.Controllers.Api {
    /// <summary>
    /// 红钻马力
    /// </summary>
    [RoutePrefix("Red")]
    public class RedController : BaseController {
        private readonly IRedRecordService _iRedRecordService;
        private readonly IUserRedTransferService _iUserRedTransferService;
        private readonly ISettingService _iSettingService;
        public RedController(IRedRecordService iRedRecordService, IUserRedTransferService iUserRedTransferService, ISettingService iSettingService) {
            _iRedRecordService = iRedRecordService;
            _iUserRedTransferService = iUserRedTransferService;
            _iSettingService = iSettingService;
        }
        ///// <summary>
        ///// 生产钻石
        ///// </summary>
        ///// <returns>The user.</returns>
        //[Route("product"), HttpGet]
        //public async Task<ResultMessage<bool>> Product() {
        //      _iRedRecordService.ProductRed(UserId);
        //    return await ResultToJson.JsonAsync(true);
        //}
        /// <summary>
        /// 领取钻石
        /// </summary>
        /// <returns></returns>
        [Route("recive"), HttpGet]
        public async Task<ResultMessage<bool>> Recive(long id) {
            bool result = _iRedRecordService.ReciveRed(id, UserId);
            return await ResultToJson.JsonAsync(result ? 1000 : 0, result ? "领取成功" : "领取失败", result);
        }
        /// <summary>
        /// 获取钻石 打开首页调用
        /// </summary>
        /// <returns></returns>
        [Route("get_red"), HttpGet]
        public async Task<ResultMessage<List<redrecorddetail_dto>>> GetRed() {
            _iRedRecordService.ProductRed(UserId);
            var list = _iRedRecordService.GetRedRecordDetails(UserId);
            return await ResultToJson.JsonAsync(list);
        }
        /// <summary>
        /// 获取钻石和马力排行（首页排行榜）
        /// </summary>
        /// <returns></returns>
        [Route("get_red_and_horse"), HttpGet, AllowAnonymous]
        public ResultMessage<List<red_rank_dto>> GetRedAndHorsePower() {
            var list = _iRedRecordService.GetRedAndHorsePower();
            return ResultToJson.Json(list);
        }
        /// <summary>
        /// 定时获取排行数据
        /// </summary>
        /// <returns></returns>
        [Route("task_red_rank"), HttpGet, AllowAnonymous, Anonymous]
        public ResultMessage<bool> GetRedAndHorsePowerRank() {
            var list = _iRedRecordService.GetRedAndHorsePower2();
            return ResultToJson.Json(list);
        }
        /// <summary>
        /// 获取用户红钻马力及排行（首页当个用户排行）
        /// </summary>
        /// <returns></returns>
        [Route("get_user_red_rank"), HttpGet]
        public async Task<ResultMessage<user_red_dto>> GetUserRedRank() {
            var list = _iRedRecordService.GetUserRed(UserId);
            return await ResultToJson.JsonAsync(list);
        }
        /// <summary>
        ///获取用户的红钻记录或马力记录列表(我的资产)
        /// </summary>
        /// <returns></returns>
        [Route("get_user_red_record"), HttpGet]
        public async Task<ResultMessage<PagedResult<user_record_dto>>> GetUserRedRecord([FromUri] RedQuery query) {
            query.merchant_id = UserId;
            var list = _iRedRecordService.GetUserRedRecords(query);
            return await ResultToJson.JsonAsync(list);
        }

        /// <summary>
        /// 获取用户红钻冻结记录
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Route("get_user_red_frozen_record"), HttpGet]
        public async Task<ResultMessage<PagedResult<app_red_frozen_record_dto>>> GetRedFrozenRecords([FromUri]PageQuery query) {
            var list = _iRedRecordService.GetRedFrozenRecordPageList(query, UserId);
            return await ResultToJson.JsonAsync(list);
        }

        /// <summary>
        /// 获取用户的红钻 马力 邀请码 等个人数据(我的资产)
        /// </summary>
        /// <returns></returns>
        [Route("get_user_red"), HttpGet]
        public async Task<ResultMessage<red_user_dto>> GetUserRed() {
            var list = _iRedRecordService.GetUser(UserId);
            return await ResultToJson.JsonAsync(list);
        }

        /// <summary>
        /// 转赠钻石
        /// </summary>
        /// <returns></returns>
        [Route("user_red_transfer"), HttpPost]
        public async Task<ResultMessage<bool>> UserRedTransfer(user_red_transfer_model model) {
            bool result = _iUserRedTransferService.UserRedTransfer(model, UserId);
            return await ResultToJson.JsonAsync(result ? "转赠成功" : "转赠失败", result);
        }

        /// <summary>
        /// 获得转赠钻石手续费率
        /// </summary>
        /// <returns></returns>
        [Route("transfer_fee_rate"), HttpGet]
        public async Task<ResultMessage<decimal>> GetTransferFeeRate() {
            token_setting_dto token_setting = _iSettingService.GetTokenSetting();
            return await ResultToJson.JsonAsync(token_setting.token_transfer != null ? token_setting.token_transfer.transfer_fee_rate : 0);
        }

        /// <summary>
        /// 统计并更新拓垦总马力设置（定时任务调用）
        /// </summary>
        /// <returns></returns>
        [Route("StatisticsTokenTotalHorsePower"), HttpGet, AllowAnonymous, Anonymous]
        public ResultMessage<bool> StatisticsTokenTotalHorsePower() {
            _iSettingService.StatisticsTokenTotalHorsePower();
            return ResultToJson.Json(true);
        }


    }
}
